package textapp.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import textapp.dao.model.Group;
import textapp.dao.model.GroupUser;
import textapp.dao.model.User;

public class GroupUserDao {
	
	@SuppressWarnings("unchecked")
	public List<GroupUser> getGroupUserByUserId(int userId) {
		EntityManager em = EMUtil.createEntityManager();
		User u = em.find(User.class, userId);
		if (u == null) {
			em.close();
			return new ArrayList<GroupUser>();
		}
		
		Query query = em.createNamedQuery("GroupUser.getByUser");
		query.setParameter("user", u);				
		List<GroupUser> resultList = query.getResultList();
		
		em.close();
		return resultList;
				
	}
	
	public List<GroupUser> getGroupUserByGroupId(int groupId) {
		EntityManager em = EMUtil.createEntityManager();
		Group g = em.find(Group.class, groupId);
		if (g == null) {
			em.close();
			return new ArrayList<GroupUser>();
		}
		
		Query query = em.createNamedQuery("GroupUser.getByGroup");
		query.setParameter("group", g);				
		List<GroupUser> resultList = query.getResultList();
		
		em.close();
		return resultList;
	}
}
